rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)

m <- data.table(read_xlsx("../Data/AlternativeSurvey.xlsx", guess_max = 1e4))

m <- m[!((time_spent < 2 | time_spent > 30) & !is.na(time_spent))]

m <- m[!(main_reason == "我自己的投资组合的变化" & freerecall_endyear < account_open_year & is.na(account_open_year))]

m[is.na(total_wealth), total_wealth := -99]
m[is.na(total_income), total_income := -99]
m[is.na(accountcheck_freq), accountcheck_freq := -99]
m[is.na(newscheck_frequency), newscheck_frequency := -99]
m[is.na(discussion_frequency), discussion_frequency := -99]
m[is.na(wechat_group), wechat_group := -99]
m[, age_group := floor(age/10)]

m[freerecall_reason == "market up" | freerecall_reason == "own up", recall_up := 100]
m[is.na(recall_up), recall_up := 0]

# Table A.5: Cued recall in the FreeRecall block in an alternative survey
f1 <- felm(recall_up ~ ret_intraday | age_group + female + education + total_wealth + total_income + accountcheck_freq + newscheck_frequency + discussion_frequency + wechat_group, m)
f2 <- felm(recall_up ~ ret_cue_month | age_group + female + education + total_wealth + total_income + accountcheck_freq + newscheck_frequency + discussion_frequency + wechat_group, m)

stargazer(f1,f2,
          align=TRUE, dep.var.labels.include=TRUE,
          covariate.labels = c("MktRet\\textsubscript{today}", "MktRet\\textsubscript{1M}"),
          omit=c("Constant"),
          omit.stat=c("LL","ser","F"), ord.intercepts=FALSE, no.space=TRUE,
          single.row=FALSE, column.sep.width = "0pt", digits = 2)

## Figure A3
## merge in SH daily index
sh <- data.table(read_xlsx("../Data/SHIndex.xlsx", guess_max = 1e4))
sh <- sh[, .(Idxtrd01, Idxtrd05)]
names(sh) <- c("date", "SHindex")
sh <- sh[-1]
sh[, date := as.Date(date)]
setorder(sh, date)

sh[, ym := as.yearmon(date)]
sh <- sh[, .(SHindex = SHindex[.N]), by = .(ym)]
sh[, SHindex := as.numeric(SHindex)]
sh[, SHindex := SHindex]

tmp <- table(m$freerecall_beginmonth) * 10
C <- data.table(tmp)
names(C) <- c("ym", "count")
C[, ym := as.yearmon(ym)]
setorder(C, ym)
C <- merge(C, sh, by = c("ym"), all.y = T)

max(C$count, na.rm = T)
ggplot(C) +
  xlab("") +
  ylab("") +
  geom_bar(aes(x = ym, y = count), stat = "identity") +
  geom_line(aes(x = ym, y = SHindex), color = "blue") +
  theme_minimal() +
  ylim(c(0, 8000)) +
  scale_x_continuous(expand = c(0, 0), limits = c(1992, 2022))
ggsave("../Figures/pic-startmonth_alternative_survey.pdf", width = 6, height = 4)

tmp <- table(m$freerecall_endmonth) * 10
C <- data.table(tmp)
names(C) <- c("ym", "count")
C[, ym := as.yearmon(ym)]
setorder(C, ym)
C <- merge(C, sh, by = c("ym"), all.y = T)

max(C$count, na.rm = T)
ggplot(C) +
  xlab("") +
  ylab("") +
  geom_bar(aes(x = ym, y = count), stat = "identity") +
  geom_line(aes(x = ym, y = SHindex), color = "blue") +
  theme_minimal() +
  ylim(c(0, 8000)) +
  scale_x_continuous(expand = c(0, 0), limits = c(1992, 2022))
ggsave("../Figures/pic-endmonth_alternative_survey.pdf", width = 6, height = 4)